package qq.service;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

public class TuPianService {
    public static String getTuPian(String url){
        try {
            Document document = Jsoup.connect(url).get();
            // 使用 css选择器 提取列表新闻 a 标签
            // <a href="https://voice.hupu.com/nba/2484553.html" target="_blank">霍华德：夏休期内曾节食30天，这考验了我的身心</a>
            Elements elements = document.getElementsByTag("img");
            int i=0;
            int random = (int)(Math.random()*40);
            for (Element element:elements){

                // 获取图片链接
                String d_url = element.attr("src");
                if (i==random){
                    return d_url;
                }else {
                    i++;
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
